package nl.boelen.game.handler;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import nl.boelen.game.Game;
import nl.boelen.game.graphics.Screen;

public class MouseHandler extends MouseAdapter {
    private int mouseX;
    private int mouseY;
    private int mouseDraggedX;
    private int mouseDraggedY;
    private int mousePressedX;
    private int mousePressedY;

    private boolean clicked = false;
    private boolean pressed = false;
    private boolean dragged = false;
    private Game game;
    private Screen screen;

    public MouseHandler(Game game, Screen screen) {
        this.game = game;
        this.screen = screen;
        game.addMouseListener(this);
        game.addMouseMotionListener(this);
        game.addMouseWheelListener(this);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        mouseX = (int) (e.getX() / Game.SCALE);
        mouseY = (int) (e.getY() / Game.SCALE);
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent event) {
        game.getScreen().changeZoom(event.getWheelRotation() == 1);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        clicked = true;
    }

    @Override
    public void mouseDragged(MouseEvent event) {
        mouseDraggedX = (int) (event.getX() / Game.SCALE);
        mouseDraggedY = (int) (event.getY() / Game.SCALE);
        dragged = true;
    }

    @Override
    public void mousePressed(MouseEvent event) {
        pressed = true;
        mousePressedX = (int) (event.getX() / Game.SCALE);
        mousePressedY = (int) (event.getY() / Game.SCALE);
    }

    @Override
    public void mouseReleased(MouseEvent event) {
        pressed = false;
        dragged = false;
    }

    public void tick() {
        if (isDragged()) {
            int xMove = getMouseX() - getMouseDraggedX();
            int yMove = getMouseY() - getMouseDraggedY();
            screen.addOffset(xMove, yMove);
            setMouse(getMouseDraggedX(), getMouseDraggedY());
        }
    }

    public boolean isClicked() {
        return clicked;
    }

    public boolean isPressed() {
        return pressed;
    }

    public boolean isDragged() {
        return dragged;
    }

    public int getMouseX() {
        return mouseX;
    }

    public int getMouseY() {
        return mouseY;
    }

    public int getMouseDraggedX() {
        return mouseDraggedX;
    }

    public int getMouseDraggedY() {
        return mouseDraggedY;
    }

    public int getMousePressedX() {
        return mousePressedX;
    }

    public int getMousePressedY() {
        return mousePressedY;
    }

    public void setMouse(int mouseX, int mouseY) {
        this.mouseX = mouseX;
        this.mouseY = mouseY;
    }
}
